package com.example.zzm.yuejian03.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.example.zzm.yuejian03.Bean.LoginBean;
import com.example.zzm.yuejian03.Bean.UserBean;
import com.example.zzm.yuejian03.Listener.TListener;
import com.example.zzm.yuejian03.Model.LoginModel;
import com.example.zzm.yuejian03.R;

public class LoginActivity extends BaseActivity {
    EditText et_uname,et_pw;
    Button btn_login,btn_regist;
    CheckBox checkBox_remember;
    int userid;
    private SharedPreferences sp;

    TListener<LoginBean> tListener = new TListener<LoginBean>() {
        @Override
        public void onResponse(LoginBean userBean) {
            userid=userBean.getUid();
//            if(userBean.getUid().equals("0")){
//                Toast.makeText(getApplicationContext(),"请先登录",Toast.LENGTH_SHORT).show();
//            }else {
//                Toast.makeText(LoginActivity.this, userBean.getUid().toString(), Toast.LENGTH_SHORT).show();
//                showToast(userid);
            if(userBean.getError().equals("0")){
                saveUser();
                Intent intent =new Intent(LoginActivity.this,MainActivity.class);
                startActivityForResult(intent,2);
                finish();
            } else {
                Toast.makeText(getApplicationContext(),"用户名密码错误",Toast.LENGTH_SHORT).show();
            }

 //}
        }

        @Override
        public void onFail(String msg) {
        }
    };

    @Override
    void initView() {
        //this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setLayout_file(R.layout.activity_login);
        et_uname=(EditText)findViewById(R.id.username);
        et_pw=(EditText)findViewById(R.id.userpass);
        btn_login=(Button)findViewById(R.id.btn_login);
        btn_regist=(Button)findViewById(R.id.btn_regist);
        checkBox_remember=(CheckBox)findViewById(R.id.checkbox_remember);
        boolean remember = getUser_remember();
        if(remember==true) {
            String username = getName();
            String userpass = getPassword();
            et_uname.setText(username);
            et_pw.setText(userpass);
            checkBox_remember.setChecked(remember);
        }
    }

    @Override
    void initEvent() {
        //登录
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username = et_uname.getText().toString();
                String userpass = et_pw.getText().toString();
                if(et_uname.getText().toString().trim().equals("")||et_pw.getText().toString().trim().equals("")){
                    Toast.makeText(LoginActivity.this,"用户名密码不能为空!!",Toast.LENGTH_SHORT).show();
                }else {
                    LoginModel model = new LoginModel();
                    model.getLoginMessge(username,userpass, tListener);
                }
            }
        });
//跳转注册
        btn_regist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivityForResult(intent,1);
//                finish();
            }
        });

    }

    @Override
    void initData() {

    }


    public  void saveUser(){
        sp = getSharedPreferences("userInfo",MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("name", et_uname.getText().toString());
        editor.putBoolean("isChecked",checkBox_remember.isChecked());
        editor.putString("password", et_pw.getText().toString());
        editor.putInt("user_id", userid);
//        showToast(userid);
        editor.commit();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case 1:
                if(resultCode== RESULT_OK){
                    String string=data.getStringExtra("uname");
                    showToast(string);
                    et_uname.setText(string);
                }
        }
    }

}